﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Interop;
using System.Globalization;

namespace myBoard.Framework
{
    /// <summary>
    /// Interaction logic for MBMessageBox.xaml
    /// </summary>
    public partial class MBMessageBox : Window
    {
        private MessageBoxResult _result;
        private MessageBoxButton _messageBoxButton;
        private EventLogEntryType _eventLogEntryType;



        // Methods
        public MBMessageBox()
        {
            this.Init(string.Empty, string.Empty, string.Empty, EventLogEntryType.Information, MessageBoxButton.OK);
        }

        public MBMessageBox(string desc)
        {
            this.Init(desc, string.Empty, string.Empty, EventLogEntryType.Information, MessageBoxButton.OK);
        }

        public MBMessageBox(string desc, string title)
        {
            this.Init(desc, title, string.Empty, EventLogEntryType.Information, MessageBoxButton.OK);
        }

        public MBMessageBox(string desc, string title, EventLogEntryType type)
        {
            this.Init(desc, title, string.Empty, type, MessageBoxButton.OK);
        }

        public MBMessageBox(string desc, string title, string source)
        {
            this.Init(desc, title, source, EventLogEntryType.Information, MessageBoxButton.OK);
        }

        public MBMessageBox(string desc, string title, MessageBoxButton buttons)
        {
            this.Init(desc, title, string.Empty, EventLogEntryType.Information, buttons);
        }

        public MBMessageBox(string desc, string title, string source, EventLogEntryType type)
        {
            this.Init(desc, title, source, type, MessageBoxButton.OK);
        }

        public MBMessageBox(string desc, string title, string source, EventLogEntryType type, MessageBoxButton buttons)
        {
            this.Init(desc, title, source, type, buttons);
        }

        private void _cancelBt_Click(object sender, RoutedEventArgs e)
        {
            this._result = MessageBoxResult.Cancel;
        }

        private void _copyToClipboardBt_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(string.Format("Source : {0}\r\nTitle : {1}\r\nDescription : {2}", this._messageSource.Content, this._messageTitle.Text, this._messageDescription.Text));
        }

        private void _noBt_Click(object sender, RoutedEventArgs e)
        {
            this._result = MessageBoxResult.No;
            base.Close();
        }

        private void _okBt_Click(object sender, RoutedEventArgs e)
        {
            this._result = MessageBoxResult.OK;
            base.Close();
        }

        private void _yesBt_Click(object sender, RoutedEventArgs e)
        {
            this._result = MessageBoxResult.Yes;
            base.Close();
        }

        protected void Init(string desc, string title, string source, EventLogEntryType type, MessageBoxButton buttons)
        {
            try
            {
                this.InitializeComponent();
                this.MessageBoxButton = buttons;
                this.MessageSource = source;
                this.MessageTitle = title;
                this.MessageType = type;
                this.MessageDescription = desc;
                this.MessageBoxButton = buttons;
                this._result = MessageBoxResult.None;
                base.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                base.MouseDown += new MouseButtonEventHandler(this.myBoardDialogBox_MouseDown);
                this._yesBt.Click += new RoutedEventHandler(this._yesBt_Click);
                this._noBt.Click += new RoutedEventHandler(this._noBt_Click);
                this._okBt.Click += new RoutedEventHandler(this._okBt_Click);
                this._cancelBt.Click += new RoutedEventHandler(this._cancelBt_Click);
                this._copyToClipboardBt.Click += new RoutedEventHandler(this._copyToClipboardBt_Click);
                if (!((Environment.OSVersion.Version.Major >= 6) && Tools.DwmApi.DwmIsCompositionEnabled()))
                {
                    base.AllowsTransparency = true;
                }
            }
            catch(Exception ex)
            {
                ExceptionsManager.Write("MBMessageBox", ex.Message, ex.ToString(), EventLogEntryType.Error, false);
                ExceptionsManager.Write(source, title, desc, EventLogEntryType.Error, false);
            }
        }

        private void myBoardDialogBox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            base.DragMove();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            if ((Environment.OSVersion.Version.Major >= 6) && Tools.DwmApi.DwmIsCompositionEnabled())
            {
                WindowInteropHelper helper = new WindowInteropHelper(this);
                base.Background = Brushes.Transparent;
                HwndSource.FromHwnd(helper.Handle).CompositionTarget.BackgroundColor = Colors.Transparent;
                Tools.DwmApi.DwmExtendFrameIntoClientArea(helper.Handle, new Tools.DwmApi.MARGINS(-1, -1, -1, -1));
            }
        }

        // Properties
        public MessageBoxButton MessageBoxButton
        {
            get
            {
                return this._messageBoxButton;
            }
            set
            {
                this._messageBoxButton = value;
                switch (this._messageBoxButton)
                {
                    case MessageBoxButton.OK:
                        this._okBt.Visibility = Visibility.Visible;
                        this._cancelBt.Visibility = Visibility.Collapsed;
                        this._yesBt.Visibility = Visibility.Collapsed;
                        this._noBt.Visibility = Visibility.Collapsed;
                        break;

                    case MessageBoxButton.OKCancel:
                        this._okBt.Visibility = Visibility.Visible;
                        this._cancelBt.Visibility = Visibility.Visible;
                        this._yesBt.Visibility = Visibility.Collapsed;
                        this._noBt.Visibility = Visibility.Collapsed;
                        break;

                    case MessageBoxButton.YesNoCancel:
                        this._yesBt.Visibility = Visibility.Visible;
                        this._noBt.Visibility = Visibility.Visible;
                        this._okBt.Visibility = Visibility.Collapsed;
                        this._cancelBt.Visibility = Visibility.Visible;
                        break;

                    case MessageBoxButton.YesNo:
                        this._yesBt.Visibility = Visibility.Visible;
                        this._noBt.Visibility = Visibility.Visible;
                        this._okBt.Visibility = Visibility.Collapsed;
                        this._cancelBt.Visibility = Visibility.Collapsed;
                        break;
                }
            }
        }

        public string MessageDescription
        {
            get
            {
                return this._messageDescription.Text;
            }
            set
            {
                this._messageDescription.Text = value;
            }
        }

        public string MessageSource
        {
            get
            {
                return this._messageSource.Content.ToString();
            }
            set
            {
                this._messageSource.Content = value;
            }
        }

        public string MessageTitle
        {
            get
            {
                return this._messageTitle.Text;
            }
            set
            {
                this._messageTitle.Text = value;
            }
        }

        public EventLogEntryType MessageType
        {
            get
            {
                return this._eventLogEntryType;
            }
            set
            {
                this._eventLogEntryType = value;
                this._messageType.DataContext = this._eventLogEntryType;
            }
        }

        public MessageBoxResult Result
        {
            get
            {
                return this._result;
            }
            set
            {
                this._result = value;
            }
        }

    }

    public class MessageTypeColorConvertor : IValueConverter
    {
        // Methods
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                switch (((EventLogEntryType)value))
                {
                    case EventLogEntryType.Error:
                        return Brushes.Red;

                    case EventLogEntryType.Warning:
                        return Brushes.Orange;

                    case EventLogEntryType.Information:
                        return new SolidColorBrush(Color.FromRgb(0, 0xcb, 0x27));
                }
            }
            return new SolidColorBrush(Color.FromRgb(0, 0xcb, 0x27));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MessageTypeGlowColorConvertor : IValueConverter
    {
        // Methods
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                switch (((EventLogEntryType)value))
                {
                    case EventLogEntryType.Error:
                        return Colors.Red;

                    case EventLogEntryType.Warning:
                        return Colors.Orange;

                    case EventLogEntryType.Information:
                        return Color.FromRgb(0, 0xcb, 0x27);
                }
            }
            return Color.FromRgb(0, 0xcb, 0x27);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MessageTypeTextColorConvertor : IValueConverter
    {
        // Methods
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return "Information";
            }
            EventLogEntryType type = (EventLogEntryType)value;
            return Enum.GetName(typeof(EventLogEntryType), type);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 


 



}
